<?php

/**
 * Status
 *
 * @author Administrator
 * @version
 */
class Model_Tables_Status extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'status';
    protected $_status_min = 1;
    protected $_status_max = 99;

    public function setStatusMin( $status_min )
    {
        $this->_status_min = $status_min;
        return $this;
    }

    public function getStatusMin()
    {
        return $this->_status_min;
    }

    public function setStatusMax( $status_max )
    {
        $this->_status_max = $status_max;
        return $this;
    }

    public function getStatusMax()
    {
        return $this->_status_max;
    }

    public function mycheckStatusValid( $status )
    {
    	//检查状态值合法性(1-99)
    	$status = intval($status);
    	if ( ($status < $this->_status_min) )
    	{
    		return false;
    	}
    	else if ( ($status > $this->_status_max) )
        {
            return false;
        }
    	else
        {
            return true;
        }
    }

    public function myget($status, $fields = '*')
    {
//        $row = $this->fetchRow('status = ' . $status);
        $row = $this->find($status)->toArray();
        if (empty($row)) {
            throw new Exception("Count not find row $status");
        }
        return $row[0];
    }

    public function mycheckExist($status)
    {
        $row = $this->find($status)->toArray();
        if (empty($row))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public function mybrowse( $issystem=null )
    {
        if (!is_null($issystem))
        {
            $where = '`issystem`=' . $issystem;
        }
        $orderby = 'status';
        $result = $this->fetchAll($where, $orderby );
        return $result->toArray();
    }

    public function mygetsUsed()
    {
        $select = $this->select()
                ->from($this->_name,'status')
                ->order('status');
        $result = $this->fetchAll($select)->toArray();
        $arrResult = array();
        if (!empty($result))
        {
	        foreach ($result as $i)
	        {
	            $arrResult[] = $i['status'];
	        }
        }
        return $arrResult;
    }

    public function myadd( $params )
    {
        if (empty($params['status']))
        {
            //缺少参数
            return false;
        }
        if (!$this->mycheckStatusValid($params['status']))
        {
            return false;
        }
        if ($this->mycheckExist($params['status']))
        {
            //已有该状态值
            return false;
        }
        $data = array(
                'status'=> $params['status'],
                'name'=> $params['name'],
                'description'=> $params['description'],
                'issystem'=> 0);
        return $this->insert($data);
    }

    public function myupdate( $status, $params )
    {
        if (empty($status))
        {
            //缺少参数
            return false;
        }
        if (!$this->mycheckStatusValid($status))
        {
            return false;
        }
    	$info = $this->myget($status, 'issystem');
        if (!empty($info['issystem']) && 1==$info['issystem'])
        {
            //系统状态，不可修改
            return false;
        }
        if ($status!=$params['status'])
        {
	        if (!$this->mycheckStatusValid($params['status']))
	        {
	            return false;
	        }
	        if ($this->mycheckExist($params['status']))
	        {
	            //已有该状态值
	            return false;
	        }
        }
        $where = '`status`=' . $status;
        $data = array(
                'status'=> $params['status'],
                'name'=> $params['name'],
                'description'=> $params['description']);
        $this->update($data, $where);
    }

    public function myupdateName( $status, $name )
    {
        $info = $this->myget($status, 'issystem');
        if (!empty($info['issystem']) && 1==$info['issystem'])
        {
            //系统状态，不可修改
            return false;
        }
        $data = array('name'=> $name);
        $where = 'status = '.$status;
        return $this->update($data , $where );
    }

    public function myupdateDescription( $status, $description )
    {
        $info = $this->myget($status, 'issystem');
        if (!empty($info['issystem']) && 1==$info['issystem'])
        {
            //系统状态，不可修改
            return false;
        }
        $data = array('description'=> $description);
        $where = 'status = '.$status;
        $this->update($data , $where );
    }

    public function mydelete( $status )
    {
        $info = $this->myget($status, 'issystem');
        if (!empty($info['issystem']) && 1==$info['issystem'])
        {
            //系统状态，不可删除
            return false;
        }
        $where = 'status = '.$status;
        $this->delete($where);
        return true;
    }
	public function getStatusList($workflow,$processes,$base=false){
		if(!$workflow)$workflow = -1;
		if(!$processes)$processes = -1;
		$db = $this->getAdapter();
		if($base)
			$sql = "select 	status, name from status where status in(2)";
		else
			$sql = "select 	status, name from status where status in(2,3) or status in(
				select passstatus from process where workflowid = $workflow and processid in ($processes)) 
				or  status in(select rejectstatus from process where workflowid = $workflow and processid in ($processes))";
		
		$result0 = $db->fetchAll($sql);
		$result = array();
		if (!empty($result0))
		{
			foreach ($result0 as $item)
			{
				$result[$item['status']] = $item['name'];
			}
		}
		return $result;
	}
	public function getStatusListByuid($workflowid,$userid,$checktatus=true){
		$db = $this->getAdapter();
		if($checktatus){
			$sql = "select status,name from status where status in(select status from  process_status where processid in(select processid from process where workflowid = :workflowid) and 
 processid in(select value from admin_role_priv where field = 'process' and roleid in(select roleid from admin_role where userid =:userid)))";
			$result0 = $db->fetchAll($sql,array("workflowid"=>$workflowid,"userid"=>$userid));
		}else{
			$sql = "select status,name from status where status in (2,99) or status in(select rejectstatus from process where workflowid =:workflowid)";
			$result0 = $db->fetchAll($sql,array("workflowid"=>$workflowid));
		}
		$result = array();
		if (!empty($result0))
		{
			foreach ($result0 as $item)
			{
				$result[$item['status']] = $item['name'];
			}
		}
		return $result;
	}
}
